import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';


import '../data/sign_type.dart';
import '../upload_apk/upload_apk_cubit.dart';
import '../upload_apk/upload_apk_view.dart';
import 'sign_apk_bloc.dart';
import 'sign_apk_event.dart';
import 'sign_apk_state.dart';

class SignApkPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // return BlocProvider(
    //   create: (BuildContext context) => SignApkBloc()..add(InitEvent()),
    //   child: Builder(builder: (context) => _buildPage(context)),
    // );
    return _buildPage(context);
  }

  Widget _buildPage(BuildContext context) {
    return BlocConsumer(
        bloc: context.read<SignApkBloc>(),
        builder: (BuildContext context, SignApkState state) {
          print("BlocConsumerSignApkState1 Change:${state.status.statusInfo}");
          return Material(
              child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Container(
                color: Colors.blueAccent,
                width: double.infinity,
                alignment: Alignment.center,
                padding: const EdgeInsets.all(10),
                child: const Text(
                  "签名进度",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 20,
                  ),
                ),
              ),
              Column(
                children: [
                  Text(
                    "当前状态：${state.status.statusInfo}",
                    style: const TextStyle(
                      color: Colors.black54,
                      fontSize: 20,
                    ),
                  ),
                  if(state.status.isUploading)  UploadApkPage(),

                ],
              ),
              Container(
                alignment: Alignment.center,
                height: 50,
                width: double.infinity,
                child: Flex(
                  direction: Axis.horizontal,
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Expanded(
                      flex: 1,
                      child: Material(
                          color: Colors.blueAccent,
                          child: InkWell(
                            onTap: () {
                              print("GestureDetector onTap ");
                              context.read<SignInfoModel>().isHideDialog = true;
                              context
                                  .read<SignApkBloc>()
                                  .add(CancelSignEvent());
                            },
                            child: const Align(
                              widthFactor: 1,
                              alignment: Alignment.center,
                              child: Text("取消",
                                  style: TextStyle(
                                      color: Colors.white, fontSize: 20)),
                            ),
                          )),
                    ),
                    const SizedBox(
                      height: 100,
                      width: 2,
                    ),
                    Expanded(
                        flex: 1,
                        child: InkWell(
                          splashColor: state.status.isDownloadSuccess
                              ? Colors.blue
                              : Colors.transparent,
                          onTap: () {
                            if (state.status.isDownloadSuccess) {
                              context.read<SignInfoModel>().isHideDialog = true;
                            }
                          },
                          child: Ink(
                            color: state.status.isDownloadSuccess
                                ? Colors.blueAccent
                                : Colors.grey,
                            child: const DecoratedBox(
                              decoration: BoxDecoration(
                                  // gradient: LinearGradient(
                                  //     begin: Alignment.topRight,
                                  //     end: Alignment.bottomLeft,
                                  //     colors: [
                                  //   Colors.blueAccent,
                                  //   Colors.deepPurple
                                  // ])
                                  color: Colors.transparent),
                              child: Align(
                                widthFactor: 1,
                                alignment: Alignment.center,
                                child: Text(
                                  "确认",
                                  style: TextStyle(
                                      color: Colors.white, fontSize: 20),
                                ),
                              ),
                            ),
                          ),
                        )),
                  ],
                ),
              )
            ],
          ));
        },
        listener: (BuildContext context, SignApkState state) {
          print("BlocConsumerSignApkState2 Change:${state.status.statusInfo}");
          print("state.uploadProgress:${state.uploadProgress}");
          if (state.status == SignStatus.uploading) {
            context
                .read<UploadApkCubit>()
                .updateUploadProgress(state.uploadProgress);
          }
        });
  }
}
